package com.google.code.swingonfire.utility.date;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

public class DateRenderInfoYearPart extends AbstractStyledDateRenderInfoPart
{

    public DateRenderInfoYearPart(int length)
    {
        super(length, determineStyle(length));
    }

    private static DateRenderInfoStyle determineStyle(int length)
    {
        if (length == 2)
        {
            return DateRenderInfoStyle.SHORT;
        }

        return DateRenderInfoStyle.LONG;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getValue(Calendar calendar, DateFormatSymbols dateFormatSymbols, Locale locale)
    {
        String value = String.valueOf(calendar.get(Calendar.YEAR));

        if ((getStyle() == DateRenderInfoStyle.SHORT) && (value.length() > 2))
        {
            return value.substring(value.length() - 2);
        }

        return formatNumber(value);
    }

}
